import { getOppositeDirection } from "@/utils"

export default class PowerCreepExtension extends PowerCreep{
    /* 请求对穿函数 如果返回为true则可以直接移动 */
    public requireCross(direction: DirectionConstant):boolean {
        // 没有记忆说明已经凉了，直接移动
        if (!this.memory) return true
        // 拒绝对穿
        if (this.memory.standed){
            this.say('👊')
            return false
        }
        // 统一对穿
        this.say('👌')
        this.move(direction)
        return true
    }
    // 请求对穿 按照对穿等级划分 等级高的可以任意对穿等级低的，等级低的无法请求等级高的对穿，等级相等则不影响
    public requestCross(direction:DirectionConstant):OK | ERR_BUSY | ERR_NOT_FOUND{
        if (!this.memory.crossLevel) this.memory.crossLevel = 10    // 10为默认对穿等级
        // 获取目标方向一格的位置
        const fontPos = this.pos.directionToPos(direction)
        // 在出口、边界
        if (!fontPos) return ERR_NOT_FOUND
        const fontCreep = (fontPos.lookFor(LOOK_CREEPS)[0] || fontPos.lookFor(LOOK_POWER_CREEPS)[0]) as Creep | PowerCreep
        if (!fontCreep) return ERR_NOT_FOUND
        this.say("👉")
        if (fontCreep.manageCross(getOppositeDirection(direction),this.memory.crossLevel)) this.move(direction)
        return OK
    }
    public manageCross(direction:DirectionConstant,crossLevel:number):boolean{
        if (!this.memory.crossLevel) this.memory.crossLevel = 10
        if (!this.memory) return true
        if (this.memory.standed || this.memory.crossLevel > crossLevel){
            if (!(Game.time % 5)) this.say('👊')
            return false
        }
        // 同意对穿
        this.say('👌')
        this.move(direction)
        return true
    }
} 